<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript ECDH Key Exchange</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script src="BigInteger.js"></script>
        <script src="PRNG4.js"></script>
        <script src="RNG.js"></script>
        <script src="JSPoint.js"></script>
        <script src="JSField2.js"></script>
        <script src="JSField4.js"></script>
        <script src="JSField6.js"></script>
        <script src="JSField12.js"></script>
        <script src="JSParams.js"></script>
        <script src="JSCurve.js"></script>
        <script src="JSPoint2.js"></script>
        <script src="JSCurve2.js"></script>
        <script src="JSPairing.js"></script>
        <script src="JSTests.js"></script>
        <script type="text/javascript">
            function getPoint(value) {
                // If value='' =>...
                var bn = new JSParams(parseInt(value));
                var E = new JSCurve(bn);
                var G = E.pointFactory(new SecureRandom());
                clear();
                document.getElementById("PCordinateX").value = G.x;
                document.getElementById("PCordinateY").value = G.y;
            }

            function randomInt(value) {
                var rng = new SecureRandom();
                var bn = new JSParams(parseInt(document.getElementById('levelSecurity').value));
                if (value === 'Alice') {
                    var a = new BigInteger(bn.n.bitLength(), rng);
                    //var n1 = bn.n.subtract(BigInteger.ONE);
                    //document.getElementById("randomAlice").value = a.mod(n1).add(BigInteger.ONE);
                    document.getElementById("randomAlice").value = a;
                } else if (value === 'Bob') {
                    var b = new BigInteger(bn.n.bitLength(), rng);
                    //var n1 = bn.n.subtract(BigInteger.ONE);
                    //document.getElementById("randomBob").value = b.mod(n1).add(BigInteger.ONE);
                    document.getElementById("randomBob").value = b;
                }
            }

            function pubKey(value) {
                var bn = new JSParams(parseInt(document.getElementById('levelSecurity').value));
                var E = new JSCurve(bn);
                var G = new JSPoint(E, new BigInteger(document.getElementById("PCordinateX").value), new BigInteger(document.getElementById("PCordinateY").value));
                if (value === 'Alice') {
                    var a = new BigInteger(document.getElementById("randomAlice").value);
                    var A = G.multiply(a).normalize();
                    document.getElementById("PkeyAx").value = A.x;
                    document.getElementById("PkeyAy").value = A.y;
                } else if (value === 'Bob') {
                    var b = new BigInteger(document.getElementById("randomBob").value);
                    var B = G.multiply(b).normalize();
                    document.getElementById("PkeyBx").value = B.x;
                    document.getElementById("PkeyBy").value = B.y;
                }
            }

            function secKey(value) {
                var bn = new JSParams(parseInt(document.getElementById('levelSecurity').value));
                var E = new JSCurve(bn);
                var B = new JSPoint(E, new BigInteger(document.getElementById("PkeyBx").value), new BigInteger(document.getElementById("PkeyBy").value));
                var A = new JSPoint(E, new BigInteger(document.getElementById("PkeyAx").value), new BigInteger(document.getElementById("PkeyAy").value));
                if (value === 'Alice') {
                    var K = B.multiply(new BigInteger(document.getElementById("randomAlice").value)).normalize();
                    document.getElementById("SkeyAx").value = K.x;
                    document.getElementById("SkeyAy").value = K.y;
                } else if (value === 'Bob') {
                    var K = A.multiply(new BigInteger(document.getElementById("randomBob").value)).normalize();
                    document.getElementById("SkeyBx").value = K.x;
                    document.getElementById("SkeyBy").value = K.y;
                }
            }

            function clear() {
                document.getElementById("PCordinateX").value = "";
                document.getElementById("PCordinateY").value = "";
                document.getElementById("randomAlice").value = "";
                document.getElementById("randomBob").value = "";
                document.getElementById("PkeyAx").value = "";
                document.getElementById("PkeyAy").value = "";
                document.getElementById("PkeyBx").value = "";
                document.getElementById("PkeyBy").value = "";
                document.getElementById("SkeyAx").value = "";
                document.getElementById("SkeyAy").value = "";
                document.getElementById("SkeyBx").value = "";
                document.getElementById("SkeyBy").value = "";
            }
        </script>
    </head>
    <body>
        <div id="Header">
            <h1>JavaScript Library For Encryption</h1>
        </div>
        <div id="Body">
            <p>This page is a demo of the Javascript Library For Encryption.</p>
           
            <table>
                <tr>
                    <td>Security</td>
                    <td>
                        <select id="levelSecurity">
                            <option value="0">Select Security</option>
                            <option value="192">192</option>
                            <option value="224">224</option>
                            <option value="254">254</option>
                            <option value="256">256</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><input type="button" value="Generate Point" onclick="getPoint(document.getElementById('levelSecurity').value);" /></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Point</td>
                    <td>
                        <input type="text" id="PCordinateX" size="80" disabled="disabled" />
                        <input type="text" id="PCordinateY" size="80" disabled="disabled" />
                    </td>
                </tr>
            </table>
           
            <br />
            <br />
            <div id="Encryption">
               
                <table>
                    <tr>
                        <td><input type="button" value="Random value A" onclick="randomInt('Alice');" /></td>
                        <td><input type="text" id="randomAlice" size="80" disabled="disabled" /></td>
                    </tr>
                    <tr>
                        <td><input type="button" value="Random value B" onclick="randomInt('Bob');" /></td>
                        <td><input type="text" id="randomBob" size="80" disabled="disabled" /></td>
                    </tr>
                    <tr>
                        <td><input type="button" value="Public Key A" onclick="pubKey('Alice');" /></td>
                        <td>
                            <input type="text" id="PkeyAx" size="80" disabled="disabled" />
                            <input type="text" id="PkeyAy" size="80" disabled="disabled" />
                        </td>
                    </tr>
                    <tr>
                        <td><input type="button" value="Public Key B" onclick="pubKey('Bob');" /></td>
                        <td>
                            <input type="text" id="PkeyBx" size="80" disabled="disabled" />
                            <input type="text" id="PkeyBy" size="80" disabled="disabled" />
                        </td>
                    </tr>
                    <tr>
                        <td><input type="button" value="Secret Key A" onclick="secKey('Alice');" /></td>
                        <td>
                            <input type="text" id="SkeyAx" size="80" disabled="disabled" />
                            <input type="text" id="SkeyAy" size="80" disabled="disabled" />
                        </td>
                    </tr>
                    <tr>
                        <td><input type="button" value="Secret Key B" onclick="secKey('Bob');" /></td>
                        <td>
                            <input type="text" id="SkeyBx" size="80" disabled="disabled" />
                            <input type="text" id="SkeyBy" size="80" disabled="disabled" />
                        </td>
                    </tr>
                    <tr>
                        <td><input type="button" value="Encrypt" /></td>
                        <td><input type="text" id="message" size="40" /></td>
                    </tr>
                </table>
               
            </div>
        </div>
        <div id="Footer">
            <h4>Jorgenhoc - &copy; Copyright</h4>
        </div>
    </body>
</html>
